//package com.mzq.langchain4j_springboot.chat;
//
//import com.google.gson.Gson;
//import com.google.gson.JsonArray;
//import com.google.gson.JsonElement;
//import com.google.gson.JsonParseException;
//import com.mzq.langchain4j_springboot.config.ApplicationContextHelperUtil;
//import com.mzq.langchain4j_springboot.web.entity.SysUser;
//import com.mzq.langchain4j_springboot.web.mapper.SysUserMapper;
//import com.mzq.langchain4j_springboot.web.service.ISysUserService;
//
//import dev.langchain4j.data.message.ChatMessage;
//import dev.langchain4j.data.message.ChatMessageDeserializer;
//import dev.langchain4j.data.message.ChatMessageSerializer;
//import dev.langchain4j.store.memory.chat.ChatMemoryStore;
//import jakarta.annotation.Resource;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.stereotype.Component;
//
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//@Component
//public class PersistentChatMemoryStore implements ChatMemoryStore {
//
////    private final Map<Integer, List<ChatMessage>> map = new HashMap<>();
//
//    @Autowired
//    private SysUserMapper sysUserMapper;
//
//    public PersistentChatMemoryStore(SysUserMapper sysUserMapper) {
//        this.sysUserMapper = sysUserMapper;
//    }
//
////    private static final SysUserMapper sysUserMapper =
////            ApplicationContextHelperUtil.getBean(SysUserMapper.class);
//
//    @Override
//    public List<ChatMessage> getMessages(Object memoryId) {
////        ChatMessageDeserializer.messagesFromJson()
////        System.out.println("1");
////        SysUserMapper sysUserMapper= (SysUserMapper) BeanUtils.getBean(SysUserMapper.class);
//        SysUser sysUser=new SysUser();
//        sysUser.setUserId(memoryId.toString());
//        List<SysUser> sysUsers = sysUserMapper.selectUserList(sysUser);
//        SysUser sysUser1 = sysUsers.get(0);
//        List<ChatMessage> chatMessages = StringUtils.isNotBlank(sysUser1.getChatMemory()) ? ChatMessageDeserializer.messagesFromJson(sysUser1.getChatMemory()) : new ArrayList<>();
////        System.out.println("chatMessages=>"+chatMessages);
//        return chatMessages;
//    }
//
//    @Override
//    public void updateMessages(Object memoryId, List<ChatMessage> messages) {
////        String s = ChatMessageSerializer.messagesToJson(messages);
////        System.out.println("2");
////        SysUserMapper sysUserMapper= (SysUserMapper) BeanUtils.getBean(SysUserMapper.class);
//        // 限制消息条数
//        final int MAX_MESSAGES = 2;
//        List<ChatMessage> limitedMessages = messages.size() > MAX_MESSAGES
//                ? messages.subList(messages.size() - MAX_MESSAGES, messages.size())
//                : messages;
//        // todo 根据memoryId修改、新增记录
//        SysUser sysUser=new SysUser();
//        sysUser.setUserId(memoryId.toString());
////        Gson gson=new Gson();
//        sysUser.setChatMemory(ChatMessageSerializer.messagesToJson(messages));
//        sysUserMapper.updateUser(sysUser);
//    }
//
//    @Override
//    public void deleteMessages(Object memoryId) {
////        System.out.println("3");
////        SysUserMapper sysUserMapper= (SysUserMapper) BeanUtils.getBean(SysUserMapper.class);
//        // todo 根据memoryId删除
//        SysUser sysUser=new SysUser();
//        sysUser.setUserId((String) memoryId);
//        sysUser.setChatMemory("");
//        sysUserMapper.updateUser(sysUser);
//    }
//
//
//    /**
//     * 将JSON字符串转换为特定类型的List。
//     *
//     * @param json      JSON字符串
//     * @param itemType  列表中项的类型
//     * @param <T>       列表中项的类型
//     * @return          转换后的List
//     */
//    public static <T> List<T> fromJsonToList(String json, Class<T> itemType) {
//        List<T> resultList = new ArrayList<>();
//        Gson gson=new Gson();
//        // 将JSON字符串转换为JsonElement
//        JsonElement jsonElement = gson.fromJson(json, JsonElement.class);
//        if (!jsonElement.isJsonArray()) {
//            throw new JsonParseException("JSON is not a JSON array");
//        }
//
//        // 将JsonElement转换为JsonArray
//        JsonArray jsonArray = jsonElement.getAsJsonArray();
//
//        // 遍历JsonArray，将每个元素转换为指定类型
//        for (JsonElement element : jsonArray) {
//            T item = gson.fromJson(element, itemType);
//            resultList.add(item);
//        }
//
//        return resultList;
//    }
//}
